using System;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using LocalBoard.Domain;

namespace LocalBoard.Persistence.SqlAccessor
{
	/// <summary>
	/// Summary description for SqlProfileAccessor.
	/// </summary>
	public class SqlProfileAccessor : SqlDataAccessBase, IProfileAccessor
	{
		public static readonly SqlProfileAccessor Instance = new SqlProfileAccessor();

		private SqlProfileAccessor()
		{
		}
		#region IProfileAccessor Members

		public System.Collections.ArrayList GetAllUserProfiles()
		{
			using(SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetAllUserProfiles"))
			{
				ArrayList users = new ArrayList();
				while(dataReader.Read())
				{
					users.Add(ProfileMapper(dataReader));
				}
				return users;
			}
		}

		public LocalBoard.Domain.Profile Get(int id)
		{
			using(SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetUserProfileById", id))
			{
				// Execute the stored procedure
				if(dataReader.Read())
					return ProfileMapper(dataReader);
			}

			return null;
		}

		public LocalBoard.Domain.Profile GetProfileByUserId(string userId)
		{
			using(SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetUserProfileByUserId", userId))
			{
				// Execute the stored procedure
				if(dataReader.Read())
					return ProfileMapper(dataReader);
			}

			return null;
		}

		public LocalBoard.Domain.Profile GetProfileByUserIdAndPassword(string userId, string password)
		{
			using(SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetUserProfileByUserIdAndPassword", userId, password))
			{
				// Execute the stored procedure
				if(dataReader.Read())
					return ProfileMapper(dataReader);
			}

			return null;
		}

		private Profile ProfileMapper(SqlDataReader dataReader)
		{
			SqlUserRoleAccessor roleAccessor = SqlUserRoleAccessor.Instance;
	
			return Profile.LoadProfileFromPersistentStore(
				DataConverter.GetInteger(dataReader["Id"]),
				DataConverter.GetString(dataReader["UserId"]),
				DataConverter.GetString(dataReader["UserName"]),
				DataConverter.GetString(dataReader["Password"]),
				DataConverter.GetString(dataReader["Email"]),
				DataConverter.GetString(dataReader["PhotoPath"]),
				DataConverter.GetString(dataReader["CustomInfo"]),
				roleAccessor.GetRolesByProfileId(DataConverter.GetInteger(dataReader["Id"]))
				);
		}

		public Profile GetAnonymousUser()
		{
			return GetProfileByUserId("Anonymous");
		}

		public int Insert(LocalBoard.Domain.Profile profile)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "InsertProfile", profile.UserId, profile.UserName, profile.Password, profile.Email, profile.PhotoPath, profile.CustomInfo));
		}

		public void Update(LocalBoard.Domain.Profile profile)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "UpdateProfile", profile.Id, profile.UserId, profile.UserName, profile.Password, profile.Email, profile.PhotoPath, profile.CustomInfo);
		}

		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "DeleteProfile", id);
		}

		#endregion
	}
}
